﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace MechaShark
{
    class Camera
    {
        //-------------Members-------------
        public Matrix currentView;
        public Matrix currentProj;
        public float cameraArc = 0;
        public float cameraRotation = 0;
        public float cameraDistance = 100;

        public float xpos = 0;
        public float ypos = 0;
        bool pers = false;
        bool pressed = false;

        //-------------Methods-------------

        public Camera()
        {

        }

        // update Camera
        #region camera update
        public void Update(GameTime gameTime, GamePadState currentGamePadState)
        {
            float time = (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            // Check for input to rotate the camera up and down around the model.

            cameraArc += currentGamePadState.ThumbSticks.Right.Y * time * 0.10f;

            //Rotation computation just in case we need it in the future
            //cameraRotation += currentGamePadState.ThumbSticks.Right.X * time * 0.10f;

            // Limit the arc movement. ** now unlimited **
            /*
            if (cameraArc > 90.0f)
                cameraArc = 90.0f;
            else if (cameraArc < -90.0f)
                cameraArc = -90.0f;
            */

            if (currentGamePadState.IsButtonDown(Buttons.A) && !pressed)
            {
                pers = !pers;
                pressed = true;
            }

            if (currentGamePadState.IsButtonUp(Buttons.A))
            {
                pressed = false;
            }

            xpos += currentGamePadState.ThumbSticks.Left.X * time * 0.10f;
            ypos += currentGamePadState.ThumbSticks.Left.Y * time * 0.10f;

            // Check for input to zoom camera in and out.

            cameraDistance += currentGamePadState.Triggers.Left * time * 0.5f;
            cameraDistance -= currentGamePadState.Triggers.Right * time * 0.5f;

            // Limit the camera distance.
            if (cameraDistance > 500.0f)
                cameraDistance = 500.0f;
            else if (cameraDistance < 10.0f)
                cameraDistance = 10.0f;

            if (currentGamePadState.Buttons.RightStick == ButtonState.Pressed)
            {
                cameraArc = 0;
                cameraRotation = 0;
                cameraDistance = 100;
            }
        }
        #endregion

        // Compute camera matrices.
        public void ComputeMatricesToDraw(GraphicsDevice Device)
        {
              currentView = Matrix.CreateRotationY(MathHelper.ToRadians(90)) * //90 degrees to cameraRotation if we want rotation
              Matrix.CreateRotationX(MathHelper.ToRadians(cameraArc)) *
              Matrix.CreateTranslation(xpos, ypos, -cameraDistance);

            if (pers)
            {
                currentProj = Matrix.CreateOrthographic(200, 200, 1, 1000);
            }
            else
            {
                currentProj = Matrix.CreatePerspectiveFieldOfView(MathHelper.Pi / 4,
                                                                    Device.Viewport.AspectRatio,
                                                                    1,
                                                                    1000);
            }
        }
    }
}
